package com.sky.aspect;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * ClassName: ResultAspect
 * Description:
 *
 * @Author: Jdz
 * @Create: 2024/1/24 - 0:26
 */
@Component
@Aspect
@Slf4j
@RequiredArgsConstructor
public class ResultAspect {


    private final LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer;

    @Before("com.sky.aspect.Pointcuts.args()")
    public void logArgs(JoinPoint joinPoint) {
//      log.info("\u001B[34m[Method args]: ==>{}<==\u001B[0m", joinPoint.getArgs());
        // 获取方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

        // 获取方法
        Method method = methodSignature.getMethod();

        // 获取参数值
        Object[] args = joinPoint.getArgs();
        // 获取参数名称
        String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);

        // 打印参数信息
        if (parameterNames != null) {
            for (int i = 0; i < args.length; i++) {
                log.info("\u001B[34m[Method args]: {} = {} \u001B[0m", parameterNames[i], args[i]);
            }
        }
    }

    @AfterReturning(value = "com.sky.aspect.Pointcuts.dml()", returning = "res")
    public void dml(JoinPoint joinPoint, boolean res) {

    }
}
